//
//  GetInfo.m
//  Bibliotecha
//
//  Created by Jaime Garnica on 4/23/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "GetInfo.h"


@implementation GetInfo

#pragma mark Accessor Methods

-(NSString *) oldBook
{
	return oldBook;
}


-(void) setOldBook: (NSString *)aBook
{
	[aBook retain];
	[oldBook release];
	oldBook = aBook;
}


-(NSArray *)oldBooks
{
	return oldBooks;
}


-(void) setOldBooks: (NSArray *)books
{
	[books retain];
	[oldBooks release];
	oldBooks = books;
}

#pragma mark Received Actions

-(IBAction) updateBook:(id)sender
{
	if( oldBooks == nil )
	{
		NSArray *updatedBook = [NSArray arrayWithObject:[titleField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[authorField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[yearField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[typeField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[categoryField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[subjectField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[urlField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:@"NO"];
		updatedBook = [updatedBook arrayByAddingObject:[pathField stringValue]];
		updatedBook = [updatedBook arrayByAddingObject:[[theLibrary returnBook:oldBook] objectAtIndex:9]];
		
		[theLibrary modifyBook:[self oldBook] withData:updatedBook];
		[theLibrary saveLibraryFile];
		[theLibrary setCurrentBook:[updatedBook objectAtIndex:0]];
		
		[editInfo close];
	}
	
	else if( [self oldBook] == nil )
	{
		NSArray *updatedBook;
		for( int i=0; i<[[self oldBooks] count]; i++ )
		{
			NSArray *tempOldBook = [theLibrary returnBook:[[self oldBooks] objectAtIndex:i]];
			
			if( [titleCheck state] == NSOnState ) updatedBook = [NSArray arrayWithObject:[multTitleField stringValue]];
			else updatedBook = [NSArray arrayWithObject:[tempOldBook objectAtIndex:0]];
			
			if( [authorCheck state] == NSOnState ) updatedBook = [updatedBook arrayByAddingObject:[multAuthorField stringValue]];
			else updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:1]];
			
			if( [yearCheck state] == NSOnState ) updatedBook = [updatedBook arrayByAddingObject:[multYearField stringValue]];
			else updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:2]];
			
			if( [typeCheck state] == NSOnState ) updatedBook = [updatedBook arrayByAddingObject:[multTypeField stringValue]];
			else updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:3]];
			
			if( [categoryCheck state] == NSOnState ) updatedBook = [updatedBook arrayByAddingObject:[multCategoryField stringValue]];
			else updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:4]];
			
			if( [subjectCheck state] == NSOnState ) updatedBook = [updatedBook arrayByAddingObject:[multSubjectField stringValue]];
			else updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:5]];
			
			updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:6]];
			updatedBook = [updatedBook arrayByAddingObject:@"NO"];
			updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:8]];
			updatedBook = [updatedBook arrayByAddingObject:[tempOldBook objectAtIndex:9]];
			
			[theLibrary modifyBook:[oldBooks objectAtIndex:i] withData:updatedBook];
			[theLibrary setCurrentBook:[updatedBook objectAtIndex:0]];
		}
		[theLibrary saveLibraryFile];
		[multEditInfo close];
	}
	[[NSNotificationCenter defaultCenter] postNotificationName:@"InfoChanged" object:self];
}


-(IBAction) discardChanges:(id)sender
{
	if( [self oldBooks] == nil ) [editInfo close];
	if( [self oldBook] == nil ) [multEditInfo close];
}


#pragma mark Methods

-(void) initWithLibrary:(LibraryController *)aLibrary andBook:(NSString *)aBook
{
	theLibrary = aLibrary;
	[self setOldBook:aBook];
	
	NSArray *bookInfo = [theLibrary returnBook:aBook]; //get the book info to display
	
	[NSBundle loadNibNamed:@"EditInfo" owner:self];	//get me an info window!
	
	[editInfo setExcludedFromWindowsMenu:YES];
	[editInfo makeKeyAndOrderFront:nil];
	
	[titleField setStringValue:[bookInfo objectAtIndex:0]];
	[authorField setStringValue:[bookInfo objectAtIndex:1]];
	[typeField setStringValue:[bookInfo objectAtIndex:3]];
	[categoryField setStringValue:[bookInfo objectAtIndex:4]];
	[subjectField setStringValue:[bookInfo objectAtIndex:5]];
	[urlField setStringValue:[bookInfo objectAtIndex:6]];
	[pathField setStringValue:[bookInfo objectAtIndex:8]];
	[yearField setStringValue: [bookInfo objectAtIndex:2]];
}


-(void) initWithLibrary:(LibraryController *)aLibrary andBooks:(NSArray *)books
{
	theLibrary = aLibrary;
	[self setOldBooks:books];
	
	[NSBundle loadNibNamed:@"EditMultipleInfo" owner:self];
	[multEditInfo setExcludedFromWindowsMenu:YES];
	[multEditInfo makeKeyAndOrderFront:nil];
	
	//check whether the title is the same for all selected books
	
	NSArray *textFields = [NSArray arrayWithObject:multTitleField];
	textFields = [textFields arrayByAddingObject:multAuthorField];
	textFields = [textFields arrayByAddingObject:multYearField];
	textFields = [textFields arrayByAddingObject:multTypeField];
	textFields = [textFields arrayByAddingObject:multCategoryField];
	textFields = [textFields arrayByAddingObject:multSubjectField];
	//url and paths not needed, since the files are ostensibly different...
	
	NSArray *checks = [NSArray arrayWithObject:titleCheck];
	checks = [checks arrayByAddingObject:authorCheck];
	checks = [checks arrayByAddingObject:yearCheck];
	checks = [checks arrayByAddingObject:typeCheck];
	checks = [checks arrayByAddingObject:categoryCheck];
	checks = [checks arrayByAddingObject:subjectCheck];
	
	BOOL kosher = YES;
	int i = 1;
	for( int j=0; j<6; j++ )
	{
		NSString *temp = [[theLibrary returnBook:[[self oldBooks] objectAtIndex:0]] objectAtIndex:j]; //get first title
		kosher = YES;
		i=1;
		while( (i<[oldBooks count]) && kosher )
		{
			if( ![temp isEqualToString:[[theLibrary returnBook:[[self oldBooks] objectAtIndex:i]] objectAtIndex:j]] )
			{
				kosher = NO;
			}
			i++;
		}
	
	
		if( kosher )
		{
			[[textFields objectAtIndex:j] setStringValue:temp];
			[[checks objectAtIndex:j] setState:NSOnState];
		}
	
		else
		{
			[[textFields objectAtIndex:j] setStringValue:@""];
			[[checks objectAtIndex:j] setState:NSOffState];
		}
	}
}


#pragma mark Method Overrides

-(void) dealloc
{
	//[theLibrary release];
	[oldBook release];
	[oldBooks release];
	[super dealloc];
}
@end
